*************************************************
*			02_eia860_capacity					*
*												*
*	Clean generation data from EIA860			*
*												*
*	Raw: 	EIA 860 (2021)						*
*	Output: facility_capacity_2021.dta			*
*												*
*************************************************



* Program to clean variable names
cap program drop fix_id_vars
program define fix_id_vars
  qui{

    cap rename *, lower
	forval i=1/5{
	cap rename *_* **
    }
	
    
	*facilityid
	cap ren plantcode facilityid
  	cap ren plntcode facilityid
  
	*facilityname
	cap ren plantname facilityname 
  	cap ren plntname  facilityname 
  
	*Generatorid
	cap ren gencode		generatorid
	
	*Type
	cap ren primemover type
	
  }
end


***
* Import data for opperating facilities in 2021
***

import excel "${wind}/data_raw/eia_860_2021/3_2_Wind_Y2021.xlsx" ///
			, sheet("Operable") firstrow cellrange(A2) clear

* Clean variables			
fix_id_vars
keep facilityid operatingyear  operatingmonth nameplatecapacitymw

* Standardize variable types for appending
cap tostring  operatingyear  operatingmonth, replace
destring nameplatecapacitymw , replace

* Create start and end date
gen open_date_860 = date(operatingyear + "/" + operatingmonth, "YM")
replace open_date_860 = mofd(open_date)
format open_date_860 %tm

gen close_date_860 = .

tempfile operating
save `operating'



* Import  data for retired facilities in 2021
import excel "${wind}/data_raw/eia_860_2021/3_2_Wind_Y2021.xlsx" ///
			, sheet("Retired and Canceled") firstrow cellrange(A2) clear

* Clean variables
fix_id_vars
keep facilityid operatingyear  operatingmonth nameplatecapacitymw retirementmonth retirementyear 

* Standardize variable types for appending
cap tostring  operatingyear  operatingmonth retirementmonth retirementyear , replace
destring nameplatecapacitymw, replace

* Create start and end date
gen open_date_860 = date(operatingyear + "/" + operatingmonth, "YM")
replace open_date_860 = mofd(open_date)
format open_date_860 %tm

gen close_date_860 = date(retirementyear	 + "/" + retirementmonth, "YM")
replace close_date_860 = mofd(close_date_860)
format close_date_860 %tm


***
* Combine and Standardize 
***

* Add operating 
append using `operating'

* Collapse Capacity at facility level where needed
collapse (sum) nameplatecapacitymw , by(facilityid)

*Label
label var nameplatecapacitymw "Nameplate Capacity (MW)"

* Save
save  "${wind}/temp/facility_capacity_2021.dta"  , replace

